<?php
include "./config/connection.php";
 
 
#setting judul laporan dan header tabel
$judul = "LAPORAN TRANSAKSI PER PERIODE PEMESANAN HANJUANG";
 
#sertakan library FPDF dan bentuk objek
require_once ("fpdf17/fpdf.php");
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
 
$pdf->Image('assets/img/logo_laporan.png',10,6);
#tampilkan judul laporan
$pdf->SetFont('Arial','B','16');
$pdf->SetY(50);
$pdf->Cell(0,10, $judul, '0', 1, 'C');
 
#buat header tabel
$pdf->SetFont('Arial','','10');
$pdf->SetTextColor(255);
$pdf->SetDrawColor(128,0,0);
$pdf->Ln();
 
#tampilkan data tabelnya
$pdf->SetTextColor(0);
$pdf->SetFont('');
$fill=false;

#query data
$query = "SELECT tanggal,id_pemesanan,nama_pemesan,alamat,status FROM pemesanan WHERE tanggal='$_POST[tanggal]' ORDER BY id_pemesanan";	
$sql = mysql_query ($query);
while ($data = mysql_fetch_array($sql)) {
	$pdf->SetFillColor(158,81,40);
	$pdf->Cell(30,5, 'Nomor Pesanan',0,0,'L',0); $pdf->Cell(5,5,':',0,0,'L',0); $pdf->Cell(90, 5,$data['id_pemesanan'],0,0,'L',0);
	$pdf->Cell(30,5, 'Nama Pemesan',0,0,'L',0); $pdf->Cell(5,5,':',0,0,'L',0); $pdf->Cell(28, 5,$data['nama_pemesan'],0,1,'L',0);
	
	$pdf->Cell(30,5, 'Tanggal',0,0,'L',0); $pdf->Cell(5,5,':',0,0,'L',0); $pdf->Cell(90, 5,$data['tanggal'],0,0,'L',0);
	$pdf->Cell(30,5, 'Status',0,0,'L',0); $pdf->Cell(5,5,':',0,0,'L',0); $pdf->Cell(28, 5,$data['status'],0,1,'L',0);
	
	$pdf->Cell(8,5, 'No',1,0,'C',1);  
	$pdf->Cell(50,5,'Nama Produk',1,0,'C',1);
	$pdf->Cell(30,5,'Harga(RP)',1,0,'C',1);
	$pdf->Cell(30,5,'Jumlah Beli',1,0,'C',1);
	$pdf->Cell(30,5,'Berat (kg)',1,0,'C',1);
	$pdf->Cell(40,5,'Sub Total(RP)',1,1,'C',1);
	
	$rp=mysql_query("SELECT * FROM pemesanan_detail A, produk B WHERE A.id_produk = B.id_produk AND id_pemesanan = '$data[id_pemesanan]'");
	$no=1;
	$pdf->SetFillColor(235,155,57);
	
	$total_beli = 0; $total_jumlah = 0; $total_berat = 0; $ongkoskirim=0; $grand_total=0;
	while($dp=mysql_fetch_array($rp)){
	
	$berat =$dp['berat']/1000*$dp['jumlah']; 
	//ongkos kirim
            $ongkos=mysql_fetch_array(mysql_query("SELECT * FROM ekspedisi,pemesanan WHERE pemesanan.id_ekspedisi=ekspedisi.id_ekspedisi AND id_pemesanan='$data[id_pemesanan]'"));
			
            $ongkoskirim1=$ongkos['harga'];
            
		$pdf->Cell(8,5,$no,1,0,'C',$fill); 
		$pdf->Cell(50,5,$dp['nama_produk'],1,0,'L',$fill);
		$pdf->Cell(30,5,$dp['harga'],1,0,'L',$fill);
		$pdf->Cell(30,5,$dp['jumlah'],1,0,'C',$fill);
		$pdf->Cell(30,5,$berat,1,0,'C',$fill);
		$pdf->Cell(40,5,$dp['totalbeli'],1,1,'C',$fill);
		$fill = !$fill;
		$no++;
		$total_beli += $dp['totalbeli'];
		$total_jumlah += $dp['jumlah'];
		$total_berat += $berat;
		$ongkoskirim=$ongkoskirim1 * $total_berat;
		$grand_total=$ongkoskirim+$total_beli;
		
	}
	
	$pdf->SetFillColor(158,81,40);
	$pdf->Cell(88,5,'TOTAL',1,0,'L',1); 
	$pdf->Cell(30,5,$total_jumlah,1,0,'C',1); 
	$pdf->Cell(30,5,$total_berat,1,0,'C',1); 
	$pdf->Cell(40,5,$total_beli,1,1,'C',1); 

	$pdf->SetFillColor(158,81,40);
	$pdf->Cell(148,5,'ONGKOS KIRIM',1,0,'L',1); 
	$pdf->Cell(40,5,$ongkoskirim,1,1,'C',1); 
	
	$pdf->SetFillColor(158,81,40);
	$pdf->Cell(148,5,'GRAND TOTAL',1,0,'L',1); 
	$pdf->Cell(40,5,$grand_total,1,1,'C',1); 
	$pdf->Ln(10);

}
$pdf->Ln(10);
$pdf->Cell(175,5, 'Bandung, '.date ("d-m-Y"),0,1,'R',0);
$pdf->Ln(30);
$pdf->Cell(185,5, '(.................................................) ',0,1,'R',0);
#output file PDF
$pdf->Output();
?>